package com.kang.controller;

import com.kang.entity.Essay;
import com.kang.service.EssayService;
import com.kang.util.WebResponse;
import com.kang.vo.ParamInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "文章接口")
@RequestMapping("/user/essay")
@Slf4j
public class EssayController {

    @Autowired
    private EssayService essayService;

    @ApiOperation("分页查询文章接口")
    @GetMapping("getEssayList")
    public WebResponse getEssayList(ParamInfo paramInfo) {
        try {
            return WebResponse.resSuccess("分页查询文章接口成功",essayService.getEssayList(paramInfo));
        } catch (Exception ex) {
            log.error("分页查询文章接口",ex);
            return WebResponse.resFail("分页查询文章接口",null);
        }
    }

    @ApiOperation("查看文章详情")
    @GetMapping("getEssayById")
    public WebResponse getEssayById(Integer id) {
        try {
            return WebResponse.resSuccess("查看文章详情接口成功",essayService.getEssayById(id));
        } catch (Exception ex) {
            log.error("查看文章详情接口失败",ex);
            return WebResponse.resFail("查看文章详情接口失败",null);
        }
    }

    @ApiOperation("发布文章")
    @PostMapping("insertEssay")
    public WebResponse insertEssay(@RequestBody Essay essay) {
        try {
            return WebResponse.resSuccess("发布文章接口成功",essayService.insertEssay(essay));
        } catch (Exception ex) {
            log.error("发布文章接口失败",ex);
            return WebResponse.resFail("发布文章接口失败",null);
        }
    }

    @ApiOperation("删除个人文章")
    @PostMapping("deleteEssay")
    public WebResponse deleteEssay(@RequestBody Essay essay) {
        try {
            return WebResponse.resSuccess("删除个人文章接口成功",essayService.deleteEssay(essay.getId()));
        } catch (Exception ex) {
            log.error("发布文章接口失败",ex);
            return WebResponse.resFail("删除个人文章接口失败",null);
        }
    }

    @ApiOperation("查看推荐文章")
    @GetMapping("getRecommendEssay")
    public WebResponse getRecommendEssay() {
        try {
            return WebResponse.resSuccess("查看推荐文章成功",essayService.getRecommendEssay());
        } catch (Exception ex) {
            log.error("查看推荐文章失败",ex);
            return WebResponse.resFail("查看推荐文章失败",null);
        }
    }

    @ApiOperation("查看推荐文章列表")
    @GetMapping("getRecommendPage")
    public WebResponse getRecommendPage(ParamInfo paramInfo) {
        try {
            return WebResponse.resSuccess("查看推荐文章成功",essayService.getRecommendPage(paramInfo));
        } catch (Exception ex) {
            log.error("查看推荐文章失败",ex);
            return WebResponse.resFail("查看推荐文章失败",null);
        }
    }

    @ApiOperation("统计热搜")
    @GetMapping("getHotEssay")
    public WebResponse getHotEssay() {
        try {
            return WebResponse.resSuccess("统计热搜成功",essayService.getHotEssay());
        } catch (Exception ex) {
            log.error("统计热搜失败",ex);
            return WebResponse.resFail("统计热搜失败",null);
        }
    }








}
